package org.wlgzs.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wlgzs.shop.bean.Coupons;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.service.ICouponsService;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ben
 * @Date: 2019/5/21 21:45
 * @Project org.wlgzs.shop
 */
@Api(tags = "这是红包的接口", value = "CouponsController")
@Controller
@RequestMapping("/coupons")
public class CouponsController {

    @Autowired
    ICouponsService couponsService;

    private static final Logger logger = LoggerFactory.getLogger(CouponsController.class);

    /**
     *增加红包信息
     * @param coupons
     * @param result
     * @return Msg
     */
    @ResponseBody
    @RequestMapping("/insertCoupons")
    @ApiOperation(value ="增加红包信息",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query",name="coupons",value ="红包信息",dataType = "Coupons")
    public Msg insertCouponsController(@Valid Coupons coupons, BindingResult result ){
        if(result.hasErrors()){
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            logger.error("订单信息增加失败");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名："+fieldError.getField());
                logger.error("错误信息："+fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorFields", map);
        }else {
        List<Coupons> returnCouponsList=couponsService.selectCouponsService(coupons.getCouponsId());
        if (returnCouponsList==null){
          int rows=couponsService.insertCouponsService(coupons);
            if (rows!=0){
                logger.info("插入成功");
                logger.info("插入了"+rows);
                return Msg.success().add("rows","插入了"+rows);
        }else{
            logger.error("插入失败");
            return Msg.fail().add("error","插入失败");
        }
        }else{
            logger.error("该红包已被创建，红包创建失败");
            return Msg.fail().add("error","该红包已被创建，红包创建失败");
        }
    }
    }

    /**
     * 根据红包id删除红包信息
     * @param couponsId
     * @return Msg
     */
    @ResponseBody
    @RequestMapping("/deleteCoupons")
    @ApiOperation(value ="根据红包id删除红包信息",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query",name="couponsId",value ="红包id",dataType = "String")
    public Msg deleteCouponsController(@Param("couponsId") String couponsId){
        if (couponsService.selectCouponsService(Integer.parseInt(couponsId))!=null){
            logger.info("该红包信息存在，可以删除");
            int rows=couponsService.deleteCouponsService(Integer.parseInt(couponsId));
            if (rows==0){
                logger.error("删除失败");
                return Msg.fail().add("error","删除失败");
            }else {
                logger.info("删除的行数为"+rows);
                return Msg.success().add("rows","删除的行数为"+rows);
            }
        }else {
            logger.error("该红包信息不存在，无法删除");
            return Msg.fail().add("error","该红包信息不存在，无法删除");
        }
    }

    /**
     * 更改红包信息
     * @param coupons
     * @param couponsId
     * @return Msg
     */
    @ResponseBody
    @RequestMapping("/updateCoupons")
    @ApiOperation(value ="更改红包信息",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query",name="coupons",value ="红包信息",dataType = "Coupons"),
    @ApiImplicitParam(paramType = "query",name="couponsId",value ="红包id",dataType = "String")
    })
    public Msg updateCouponsController(@Valid Coupons coupons,@Param("couponsId") String couponsId){
        List<Coupons> returnList=couponsService.selectCouponsService(Integer.parseInt(couponsId));
        if (returnList==null){
            logger.error("该红包未被创建，无法更改");
            return Msg.fail().add("error","该红包未被创建，无法更改");
        }else {
            int rows=couponsService.updateCouponsService(coupons);
            logger.info("红包信息更改成功");
            logger.info("更改的行数为"+rows);
            return  Msg.success().add("rows","更改的行数为"+rows);
        }
    }

    /**
     *根据红包id查询红包信息
     * @param couponsId
     * @return Msg
     */
    @ResponseBody
    @RequestMapping("/selectCoupons")
    @ApiOperation(value ="根据红包id查询红包信息",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query",name="couponsId",value ="红包id",dataType = "String")
    public Msg selectCouponsController(@Param("couponsId") String couponsId){
        if (couponsService.selectCouponsService(Integer.parseInt(couponsId))==null) {
            logger.error("该用户没有红包");
            return Msg.fail().add("error", "该用户没有红包");
        }else {
            List<Coupons> couponsList=couponsService.selectCouponsService((Integer.parseInt(couponsId)));
            Iterator<Coupons> couponsIterator=couponsList.iterator();
            while(couponsIterator.hasNext()){
                logger.info("该用户的红包信息为"+couponsIterator.next());
            }
            return Msg.success().add("couponsList",couponsList);
        }
    }
}
